package com.lkj.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer_Topics2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("192.168.1.84");
        factory.setPort(5672);
        factory.setVirtualHost("/lkj");
        factory.setUsername("lkj");
        factory.setPassword("lkj");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_topics";

        channel.exchangeDeclare(exchangeName,BuiltinExchangeType.TOPIC,true,false,false,null);

        String queue1Name = "test_topics_queue1";
        String queue2Name = "test_topics_queue2";

        channel.queueDeclare(queue2Name,true,false,false,null);

        channel.queueBind(queue2Name,exchangeName,"order.info.ji");


        Consumer consumer = new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //路由key
                System.out.println("路由key为：" + envelope.getRoutingKey());
                //交换机
                System.out.println("交换机为：" + envelope.getExchange());
                //消息id
                System.out.println("消息id为：" + envelope.getDeliveryTag());
                System.out.println(new String(body));
            }
        };

        channel.basicConsume(queue2Name,true,consumer);

    }
}
